/* $Id: list.c 3553 2011-05-05 06:14:19Z nanang $ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include <dpl/list.h>
#include <dpl/dpl_assert.h>
#include <dpl/log.h>

//D_LOG_DOMAIN(test_list,"this is a list test");
D_LOG_DOMAIN(list_test_case,"this is a case, how to use");
/**
 * \page page_dpllib_samples_list_c Example: List Manipulation
 *
 * Below is sample program to demonstrate how to manipulate linked list.
 *
 * \includelineno dpllib-samples/list.c
 */

struct my_node
{
    // This must be the first member declared in the struct!
    DPL_DECL_LIST_MEMBER(struct my_node);
    int value;
};




typedef struct list_node
{
    DPL_DECL_LIST_MEMBER(struct list_node);
    int value;
} list_node;

static int compare_node(void *value, const dpl_list_type *nd)
{
    list_node *node = (list_node*)nd;
    return ((long)value == node->value) ? 0 : -1;
}

#define DPL_SIGNED_ARRAY_SIZE(a)	((int)DPL_ARRAY_SIZE(a))

int list_test_proc()
{
    list_node nodes[4];    // must be even number of nodes
    list_node list;
    list_node list2;
    list_node *p;
    int i; // don't change to unsigned!

    //
    // Test insert_before().
    //
    list.value = (unsigned)-1;
    dpl_list_init(&list);
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	nodes[i].value = i;
	dpl_list_insert_before(&list, &nodes[i]);
    }
    // check.
    for (i=0, p=list.next; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
	dpl_assert(p->value == i);
	if (p->value != i) {
	    return -1;
	}
    }

    //
    // Test insert_after()
    //
    dpl_list_init(&list);
    for (i=DPL_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
	dpl_list_insert_after(&list, &nodes[i]);
    }
    // check.
    for (i=0, p=list.next; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
	dpl_assert(p->value == i);
	if (p->value != i) {
	    return -1;
	}
    }

    //
    // Test merge_last()
    //
    // Init lists
    dpl_list_init(&list);
    dpl_list_init(&list2);
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
	dpl_list_insert_before(&list, &nodes[i]);
    }
    for (i=DPL_SIGNED_ARRAY_SIZE(nodes)/2; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	dpl_list_insert_before(&list2, &nodes[i]);
    }
    // merge
    dpl_list_merge_last(&list, &list2);
    // check.
    for (i=0, p=list.next; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
	dpl_assert(p->value == i);
	if (p->value != i) {
	    return -1;
	}
    }
    // check list is empty
    dpl_assert( dpl_list_empty(&list2) );
    if (!dpl_list_empty(&list2)) {
	return -1;
    }

    // 
    // Check merge_first()
    //
    dpl_list_init(&list);
    dpl_list_init(&list2);
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
	dpl_list_insert_before(&list, &nodes[i]);
    }
    for (i=DPL_SIGNED_ARRAY_SIZE(nodes)/2; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	dpl_list_insert_before(&list2, &nodes[i]);
    }
    // merge
    dpl_list_merge_first(&list2, &list);
    // check (list2).
    for (i=0, p=list2.next; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
	dpl_assert(p->value == i);
	if (p->value != i) {
	    return -1;
	}
    }
    // check list is empty
    dpl_assert( dpl_list_empty(&list) );
    if (!dpl_list_empty(&list)) {
	return -1;
    }

    //
    // Test insert_nodes_before()
    //
    // init list
    dpl_list_init(&list);
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
	dpl_list_insert_before(&list, &nodes[i]);
    }
    // chain remaining nodes
    dpl_list_init(&nodes[DPL_SIGNED_ARRAY_SIZE(nodes)/2]);
    for (i=DPL_SIGNED_ARRAY_SIZE(nodes)/2+1; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	dpl_list_insert_before(&nodes[DPL_SIGNED_ARRAY_SIZE(nodes)/2], &nodes[i]);
    }
    // insert nodes
    dpl_list_insert_nodes_before(&list, &nodes[DPL_SIGNED_ARRAY_SIZE(nodes)/2]);
    // check
    for (i=0, p=list.next; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
	dpl_assert(p->value == i);
	if (p->value != i) {
	    return -1;
	}
    }

    // erase test.
    dpl_list_init(&list);
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	nodes[i].value = i;
	dpl_list_insert_before(&list, &nodes[i]);
    }
    for (i=DPL_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
	int j;
	dpl_list_erase(&nodes[i]);
	for (j=0, p=list.next; j<i; ++j, p=p->next) {
	    dpl_assert(p->value == j);
	    if (p->value != j) {
		return -1;
	    }
	}
    }

    // find and search
    dpl_list_init(&list);
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	nodes[i].value = i;
	dpl_list_insert_before(&list, &nodes[i]);
    }
    for (i=0; i<DPL_SIGNED_ARRAY_SIZE(nodes); ++i) {
	p = (list_node*) dpl_list_find_node(&list, &nodes[i]);
	dpl_assert( p == &nodes[i] );
	if (p != &nodes[i]) {
	    return -1;
	}
	p = (list_node*) dpl_list_search(&list, (void*)(long)i, &compare_node);
	dpl_assert( p == &nodes[i] );
	if (p != &nodes[i]) {
	    return -1;
	}
    }
    return 0;
}

int test_list()
{
    struct my_node nodes[10];
    struct my_node list;
    struct my_node *it;
    int i;
    
    // Initialize the list as empty.
    dpl_list_init(&list);
    
    // Insert nodes.
    for (i=0; i<10; ++i) {
        nodes[i].value = i;
        dpl_list_insert_before(&list, &nodes[i]);
    }
    
    // Iterate list nodes.
    it = list.next;
    while (it != &list) {
      //  DPL_LOG(3,("list", "value = %d", it->value));
      dpl_v("list value = %d\n", it->value);
        it = it->next;
    }
    
    // Erase all nodes.
    for (i=0; i<10; ++i) {
        dpl_list_erase(&nodes[i]);
    }
    
    // List must be empty by now.
    dpl_assert( dpl_list_empty(&list) );
	
	//tc edit
	if(list_test_proc()<0){
		dpl_e("list_test_proc");
		return -1;
	}else{
		dpl_i("list_test ok");
	}
	//end tc
    return 0;
}
